<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>CocoaPods 指南 | 素墨文胤</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="何为 Cocoa ？　　Cocoa 是 Mac OS X 上的五大 API 之一，是苹果的面向对象开发框架。　　其他四大 API 分别是：　　Carbon：是苹果的非面向对象的开发框架，比 Cocoa 的层次更低，比较类似于微软视窗操作系统的 Win32 API。　　POSIX：是可移植操作系统接口（Portable Operating System Interface）的缩写，定义了操作系统应该">
<meta property="og:type" content="article">
<meta property="og:title" content="CocoaPods 指南">
<meta property="og:url" content="http://godera.org/2016/11/27/CocoaPods-指南/index.html">
<meta property="og:site_name" content="素墨文胤">
<meta property="og:description" content="何为 Cocoa ？　　Cocoa 是 Mac OS X 上的五大 API 之一，是苹果的面向对象开发框架。　　其他四大 API 分别是：　　Carbon：是苹果的非面向对象的开发框架，比 Cocoa 的层次更低，比较类似于微软视窗操作系统的 Win32 API。　　POSIX：是可移植操作系统接口（Portable Operating System Interface）的缩写，定义了操作系统应该">
<meta property="og:updated_time" content="2016-12-02T02:28:30.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="CocoaPods 指南">
<meta name="twitter:description" content="何为 Cocoa ？　　Cocoa 是 Mac OS X 上的五大 API 之一，是苹果的面向对象开发框架。　　其他四大 API 分别是：　　Carbon：是苹果的非面向对象的开发框架，比 Cocoa 的层次更低，比较类似于微软视窗操作系统的 Win32 API。　　POSIX：是可移植操作系统接口（Portable Operating System Interface）的缩写，定义了操作系统应该">
  
  
    <link rel="icon" href="/favicon.png">
  

  <link rel="stylesheet" href="/css/style.css">
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  
  
    <link rel="stylesheet" href="/scrollLoading/style.css">
  
  


  

  
    <link href='//fonts.useso.com/css?family=Titillium+Web:300,400,600' rel='stylesheet' type='text/css'>
    <link href="//fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/font-awesome/css/font-awesome.min.css">
</head>

<body>
  <div id="wrap">
    <header id="header">
  <div id="header-outer" class="outer">
    <div class="container">
      <div class="container-inner">
        <div id="header-title">
          <h1 class="logo-wrap">
            <a href="/" class="logo"></a>
          </h1>
          
            <h2 class="subtitle-wrap">
              <p class="subtitle">一日心静一日仙，不以神力乱人间。</p>
            </h2>
          
        </div>
        <div id="header-inner" class="nav-container">
          <a id="main-nav-toggle" class="nav-icon fa fa-bars"></a>
          <div class="nav-container-inner">
            <ul id="main-nav">
              
                <li class="main-nav-list-item" ><a class="main-nav-list-link" href="/">Home</a></li>
              
                    <ul class="main-nav-list"><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/JS/">JS</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/Unix-Linux/">Unix_Linux</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/iOS/">iOS</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/哲学辨思/">哲学辨思</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/数据库/">数据库</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/文字文化/">文字文化</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/生活感悟/">生活感悟</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/随心随性/">随心随性</a></li><li class="main-nav-list-item"><a class="main-nav-list-link" href="/categories/默认标签/">默认标签</a></li></ul>
                  
                <li class="main-nav-list-item" ><a class="main-nav-list-link" href="/about/index.html">About</a></li>
              
            </ul>
            <nav id="sub-nav">
              <div id="search-form-wrap">
                
                  <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><input type="hidden" name="sitesearch" value="http://godera.org"></form>
                
              </div>
            </nav>
          </div>
        </div>
      </div>
    </div>
  </div>
</header>
    <div class="container">
      <div class="main-body container-inner">
        <div class="main-body-inner">
          <section id="main">
            <div class="main-body-header">

              <h1 class="header"><a class="page-title-link" href="/categories/iOS/">iOS</a></h1>
            </div>
            <div class="main-body-content">
              
  <article id="post-CocoaPods-指南" class="article article-single article-type-post" itemscope itemprop="blogPost">
    <div class="article-inner">
      <!--  -->
      
        <header class="article-header">
          
  
    <h1 class="article-title" itemprop="name">
      CocoaPods 指南
    </h1>
  

        </header>
      
      <p class="article-byline">
        <a href="/2016/11/27/CocoaPods-指南/" class="article-date">
  <time datetime="2016-11-27T11:03:26.000Z" itemprop="datePublished">2016-11-27</time>
</a>
      </p>
      <div class="article-entry" itemprop="articleBody">
        <h5 id="何为_Cocoa_？">何为 Cocoa ？</h5><p>　　Cocoa 是 Mac OS X 上的五大 API 之一，是苹果的<em>面向对象</em>开发框架。<br>　　其他四大 API 分别是：<br>　　Carbon：是苹果的<em>非面向对象</em>的开发框架，比 Cocoa 的层次更低，比较类似于微软视窗操作系统的 Win32 API。<br>　　POSIX：是可移植操作系统接口（Portable Operating System Interface）的缩写，定义了操作系统应该为应用程序提供的接口标准，其正式称呼为 IEEE 1003，国际标准名称为 ISO/IEC 9945。<br>　　X11：也叫 X Window 系统，是一种位图显示的视窗系统，它是在 Unix 和 Unix-like 操作系统以及 OpenVMS 上建立图形用户界面的标准工具包和协议。<br>　　Java：苹果提供了 Java 到 Cocoa 的绑定，但是在开发者中并未得到广泛采用.</p>
<h5 id="何为_CocoaPods_？">何为 CocoaPods ？</h5><p>　　CocoaPods：Cocoa 的依赖管理器，用来管理 Xcode 工程的依赖关系。<br>　　我们指定依赖关系时只需要在 Podfile 文件里书写即可。CocoaPods 会递归解决依赖，并且创建、关联 Xcode workspace 来建立项目。</p>
<h5 id="CocoaPods_的原理">CocoaPods 的原理</h5><p>　　CocoaPods 将所有的依赖库都放到另一个名为 Pods 的项目中，然后让主项目依赖 Pods 项目，这样，源码管理工作都从主项目移到了 Pods 项目中。Pods 项目最终会编译成一个名为 libPods.a 的文件，主项目只需要依赖这个 .a 文件即可。</p>
<h5 id="安装_CocoaPods">安装 CocoaPods</h5><p>　　CocoaPods 使用 Ruby 建立的，所以要先安装 Ruby 环境。（淘宝 Ruby 镜像已不可用）<br>　　RubyGems 是 Ruby 的程序包管理器。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$ gem sources --remove https://rubygems.org/ 【删除国外 gem 源】</div><div class="line">$ gem sources --add https://gems.ruby-china.org/ 【添加国内 gem 源】</div><div class="line">$ sudo gem update --system 【更新 gem 库】</div><div class="line">$ sudo gem install cocoapods【用 gem 安装 cocoapods】</div><div class="line">$ pod setup【更新 pod 依赖库，需时长】</div></pre></td></tr></table></figure></p>
<p>　　在项目根目录下用 <code>$ pod init</code> 生成“Podfile”的文件，然后书写依赖。书写完毕后执行：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ pod install【安装 Podfile 指定的 pod 依赖库】</div></pre></td></tr></table></figure></p>
<p>　　Podfile文件内容的格式应该如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">platform :ios, &apos;8.0&apos; 【注明你的开发平台以及版本，&apos;8.0&apos;忽略不写即为最新版本】</div><div class="line">pod &apos;AFNetworking&apos;, &apos;~&gt; 2.5.3&apos; 【&apos;~&gt; 2.5.3&apos;为版本号，忽略不写即为最新版本】</div><div class="line">......</div></pre></td></tr></table></figure></p>
<p>　　第三方库的版本号可以直接指定，比如’2.0’，也可以加修饰符，修饰符有 &gt;、&gt;=、&lt;、&lt;=、~&gt; 五种，其中“~&gt; a.b.c”表示使用大于等于 a.b.c 但小于a.(b+1).0 的版本，即该版本到下一个大版本之间的版本。<br>　　<em>注意：</em>Podfile.lock 文件记录下了当时用的 Pods 依赖库的版本供 pod install 使用，故需要加入版本管理，以避免 Pod 依赖库版本不一致。</p>
<h5 id="使用_CocoaPods_私有库架构_APP">使用 CocoaPods 私有库架构 APP</h5><p>　　CocoaPods 在小型项目中可以直接指定 AFNetworking 等第三方类库来使用，然而从架构上讲，提供给开发人员使用的不应该直接是第三方类库，而应该是使用更简单、更切合实际业务需求的私有库。<br><span id="private_pod_writting"></span></p>
<h6 id="私有库在_Podfile_中的写法如下：">私有库在 Podfile 中的写法如下：</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pod &apos;APod&apos;, :git =&gt; &apos;https://xxxx/xxx/APod.git&apos;, :tag =&gt; &apos;0.0.1&apos; 【私有 pod 库路径、版本指定】</div></pre></td></tr></table></figure>
<p></p>
<h6 id="私有库的建立">私有库的建立</h6><p>1、在 Github、Coding 或 Gitlab 上建立私有 pod 库（.git）。<br>2、在本地创建 APod 开发工程，并关联到远程库。（最好将最终提供的类放在一个文件夹里，比如 “Lib” 文件夹）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ cd 工程目录</div><div class="line">$ git init</div><div class="line">$ git remote add origin https://git.coding.net/xxx/APod.git</div></pre></td></tr></table></figure></p>
<p>3、如果要 pod 进第三方类库，就 <code>$ pod init</code> 生成 Podfile 后书写依赖，然后<code>$ pod install</code>。<br>4、在开发工程根目录下新建 .podspec 文件，命令如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ pod spec create APod</div></pre></td></tr></table></figure></p>
<p>5、编写 Spec 文件。需要注意：<br>    5.1、s.version 可以先设为远端库有的 tag 号。<br>    5.2、pod 进来的依赖要在 s.dependency 指定。<br>    5.3、s.library（系统库）、s.framework（系统框架） 要正确配置。<br>    5.4、s.source_files、s.public_header_files 需要正确配置。如果我们的输出文件都放在了 “Lib” 文件夹下，s.source_files  = “Lib/<em>.{h,m}”，s.public_header_files = “Lib/</em>.h”<br>6、验证 Spec 文件是否合格，命令如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ pod spec lint</div></pre></td></tr></table></figure></p>
<p>如果有一些小问题不通过，可以忽略。<br>7、将代码推到远端库。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">$ 改 s.version = &apos;0.0.1&apos;</div><div class="line">$ git add -A</div><div class="line">$ git commit -m &quot;Release 0.0.1.&quot;</div><div class="line"></div><div class="line">$ git pull</div><div class="line">$ 解决冲突后继续；注意确保 s.version 是自己期望的值</div><div class="line">$ git tag &apos;0.0.1&apos;</div><div class="line">$ git push --tags</div></pre></td></tr></table></figure></p>
<p>8、现在可以在其他项目中使用该 pod 了。写法见<a href="#private_pod_writting">上一节</a>。如果引用的时候用 “” 找不到头文件，就换 &lt;&gt; 试试。</p>
<h5 id="CocosPods_官方帮助页">CocosPods 官方帮助页</h5><p>　　<a href="https://guides.cocoapods.org" target="_blank" rel="external">https://guides.cocoapods.org</a></p>

      </div>
      <footer class="article-footer">
        <a data-url="http://godera.org/2016/11/27/CocoaPods-指南/" data-id="ciw7o5qgx00033refqwevz9mp" class="article-share-link"><i class="fa fa-share"></i>Share</a>
        
        
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/iOS/">iOS</a></li></ul>

      </footer>
    </div>
  </article>
  
  


<!--高速版-->
<div id="SOHUCS"></div>
<script charset="utf-8" type="text/javascript" src="http://changyan.sohu.com/upload/changyan.js" ></script>
<script type="text/javascript">
    window.changyan.api.config({
        appid: 'cys5Qvajj',
        conf: 'prod_62197afb84b42bd16c642feedaaef9f1'
    });
</script>   

            </div>
          </section>
          <aside id="sidebar">
  <a class="sidebar-toggle" title="Expand Sidebar"><i class="toggle icon"></i></a>
  <div class="sidebar-top">
    <p>follow:</p>
    <ul class="social-links">
      
        <li><a class="social-tooltip" title="github" href="https://github.com/godera" target="_blank"><i class="icon fa fa-github"></i></a></li>
      
    </ul>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/11/30/Markdown-技巧汇总（持续帖）/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">newer</strong>
      <p class="article-nav-title">
        
          Markdown 技巧汇总（持续帖）
        
      </p>
      <i class="icon fa fa-chevron-right" id="icon-chevron-right"></i>
    </a>
  
  
    <a href="/2016/04/16/OC-和-Swift-的混用方法/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">older</strong>
      <p class="article-nav-title">OC 和 Swift 的混用方法</p>
      <i class="icon fa fa-chevron-left" id="icon-chevron-left"></i>
    </a>
  
</nav>

  
  <div class="widgets-container">
    
      
  <div class="widget-wrap">
    <h3 class="widget-title">recents</h3>
    <div class="widget">
      <ul id="recent-post" class="">
        
          <li>
            
            <div class="item-thumbnail">
              
<a href="/2016/11/30/Markdown-技巧汇总（持续帖）/" class="thumbnail">
  
    <span class="thumbnail-image thumbnail-none"></span>
  
</a>

            </div>
            
            <div class="item-inner">
              <p class="item-category"><a class="article-category-link" href="/categories/iOS/">iOS</a></p>
              <p class="item-title"><a href="/2016/11/30/Markdown-技巧汇总（持续帖）/" class="title">Markdown 技巧汇总（持续帖）</a></p>
              <p class="item-date"><time datetime="2016-11-30T05:10:18.000Z" itemprop="datePublished">2016-11-30</time></p>
            </div>
          </li>
        
          <li>
            
            <div class="item-thumbnail">
              
<a href="/2016/11/27/CocoaPods-指南/" class="thumbnail">
  
    <span class="thumbnail-image thumbnail-none"></span>
  
</a>

            </div>
            
            <div class="item-inner">
              <p class="item-category"><a class="article-category-link" href="/categories/iOS/">iOS</a></p>
              <p class="item-title"><a href="/2016/11/27/CocoaPods-指南/" class="title">CocoaPods 指南</a></p>
              <p class="item-date"><time datetime="2016-11-27T11:03:26.000Z" itemprop="datePublished">2016-11-27</time></p>
            </div>
          </li>
        
          <li>
            
            <div class="item-thumbnail">
              
<a href="/2016/04/16/OC-和-Swift-的混用方法/" class="thumbnail">
  
    <span class="thumbnail-image thumbnail-none"></span>
  
</a>

            </div>
            
            <div class="item-inner">
              <p class="item-category"><a class="article-category-link" href="/categories/iOS/">iOS</a></p>
              <p class="item-title"><a href="/2016/04/16/OC-和-Swift-的混用方法/" class="title">OC 和 Swift 的混用方法</a></p>
              <p class="item-date"><time datetime="2016-04-16T11:11:42.000Z" itemprop="datePublished">2016-04-16</time></p>
            </div>
          </li>
        
          <li>
            
            <div class="item-thumbnail">
              
<a href="/2016/04/11/ReactiveCocoa-简单总结/" class="thumbnail">
  
    <span class="thumbnail-image thumbnail-none"></span>
  
</a>

            </div>
            
            <div class="item-inner">
              <p class="item-category"><a class="article-category-link" href="/categories/iOS/">iOS</a></p>
              <p class="item-title"><a href="/2016/04/11/ReactiveCocoa-简单总结/" class="title">ReactiveCocoa 简单总结</a></p>
              <p class="item-date"><time datetime="2016-04-11T03:00:29.000Z" itemprop="datePublished">2016-04-11</time></p>
            </div>
          </li>
        
          <li>
            
            <div class="item-thumbnail">
              
<a href="/2016/04/09/从汉语字面理解-观察者模式-和-订阅者模式-的区别/" class="thumbnail">
  
    <span class="thumbnail-image thumbnail-none"></span>
  
</a>

            </div>
            
            <div class="item-inner">
              <p class="item-category"><a class="article-category-link" href="/categories/Unix-Linux/">Unix_Linux</a></p>
              <p class="item-title"><a href="/2016/04/09/从汉语字面理解-观察者模式-和-订阅者模式-的区别/" class="title">从汉语字面理解 观察者模式 和 订阅者模式 的区别</a></p>
              <p class="item-date"><time datetime="2016-04-09T10:19:20.000Z" itemprop="datePublished">2016-04-09</time></p>
            </div>
          </li>
        
      </ul>
    </div>
  </div>

    
      
  <div class="widget-wrap widget-list">
    <h3 class="widget-title">archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">November 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">April 2016</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">March 2016</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/02/">February 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">December 2015</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/11/">November 2015</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/10/">October 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/09/">September 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">August 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/06/">June 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">May 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">April 2015</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/03/">March 2015</a><span class="archive-list-count">36</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/02/">February 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/01/">January 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/10/">October 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/06/">June 2012</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2011/10/">October 2011</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2010/06/">June 2010</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2008/08/">August 2008</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget-wrap widget-float">
    <h3 class="widget-title">tag cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/JS/" style="font-size: 10px;">JS</a> <a href="/tags/Unix-Linux/" style="font-size: 14px;">Unix_Linux</a> <a href="/tags/iOS/" style="font-size: 20px;">iOS</a> <a href="/tags/哲学辨思/" style="font-size: 16px;">哲学辨思</a> <a href="/tags/数据库/" style="font-size: 10px;">数据库</a> <a href="/tags/文字文化/" style="font-size: 18px;">文字文化</a> <a href="/tags/生活感悟/" style="font-size: 14px;">生活感悟</a> <a href="/tags/随心随性/" style="font-size: 12px;">随心随性</a> <a href="/tags/默认标签/" style="font-size: 10px;">默认标签</a>
    </div>
  </div>


    
      
  <div class="widget-wrap widget-list">
    <h3 class="widget-title">links</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="http://hexo.io">Hexo</a>
          </li>
        
      </ul>
    </div>
  </div>


    
  </div>
</aside>
        </div>
      </div>
    </div>
    <footer id="footer">
  
  <div class="container">
    <div class="container-inner">
      <a id="back-to-top" href="javascript:;"><i class="icon fa fa-angle-up"></i></a>
      <div class="credit">
        <h1 class="logo-wrap">
          <a href="/" class="logo"></a>
        </h1>
        <p>&copy; 2016 梁逊</p>
        <p>Powered by <a href="//hexo.io/" target="_blank">Hexo</a>. Theme by <a href="//github.com/ppoffice" target="_blank">PPOffice</a></p>
      </div>
    </div>
  </div>
</footer>
    


  <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>



  <script src="/fancybox/jquery.fancybox.pack.js"></script>



  <script src="/scrollLoading/jquery.scrollLoading.js"></script>
  <script src="/scrollLoading/main.js"></script>




<script src="/js/html-patch.js"></script>
<script src="/js/script.js"></script>

  </div>
</body>
</html>
